module D{
    export class Table implements NetMessage{
        /** 桌子ID */
        public tableID:number;
        /** 玩家列表 */
        public players:Array<PlayerData>;
        /** 游戏状态 */
        public gameStatus:number;
        /** 等待准备秒数 -1特殊值不需要自动准备 */
        public waitReady:number;
        /** 等待定时器 */
        private wait:number;

        /** 构造函数 */
        public constructor(){
            this.tableID = 65535;
            this.gameStatus = 0;
            this.waitReady = 3;
            this.players = new Array<PlayerData>();
            this.wait = 0;
        }

        /** 接收到消息 */
        public onMessage(d:any):void{
            switch(d[1]){
                case 100:{
                    this.playerEnter(d[2]);
                    break;
                }case 101:{
                    this.playerStatus(d[2]);
                    break;
                }case 102:{
                    this.playerScore(d[2]);
                    break;
                }case 112:{ //游戏房间登录完成
                    App.enterGame(new V.Buyu());
                    break;
                }
            }
        }

        /** 玩家进入 */
        public playerEnter(d:any):number{
            let pd:PlayerData;
            if(d.U == App.userData.userID) {
                pd = App.userData;
                this.tableID = d.T;
                if(pd.chairID == 65535 && d.T != 65535){ //第一次进桌子发生同步桌子
                    App.net.send([6,1,{"bAllowLookon":1}]);
                    App.net.send([6,2]);
                }
            } else pd = new PlayerData();
            pd.faceID = d.F;
            pd.userID = d.U;
            pd.gender = d.G;
            pd.chairID = d.C;
            pd.userStatus = d.S;
            pd.score = d.M;
            pd.nickName = d.A;
            if(pd.chairID != 65535){
                this.players[pd.chairID] = pd;
            } else { //退出桌子
                for(let i=0;i<this.players.length;i++){
                    if(this.players[i] && this.players[i].userID == pd.userID){
                        delete this.players[i];
                        return i;
                    }
                }
            }
            App.gameView && App.gameView.showPlayer(pd.chairID);
            return pd.chairID;
        }

        /** 玩家的游戏币变化 */
        public playerScore(d:any):void{
            for(let i=0;i<this.players.length;i++){ //当前用UserID判断其实可以直接用椅子号 待优化
                if(this.players[i] && this.players[i].userID == d.U){ 
                    this.players[i].score = d.M;
                    break;
                }
            }
        }

        /** 玩家状态变化 */
        public playerStatus(d:any):number{
            let cid = 65535, pd:PlayerData;
            if(this.players[d.C]) pd = this.players[d.C];
            else{
                if(d.U == App.userData.userID) pd = App.userData;
                else{
                    for(let i=0;i<this.players.length;i++){
                        if(this.players[i] && this.players[i].userID == d.U) pd = this.players[i];
                    }
                }
                if(d.C == 65535) delete this.players[pd.chairID];
            }
            cid = pd.chairID;
            if(d.S == UserStatus.NULL){ //退出房间
                this.players = [];
                if(this.wait) egret.clearTimeout(this.wait);
                this.wait = null;
            }else if(d.U == App.userData.userID && d.S == UserStatus.SIT){ //收到坐下
                console.log("this.waitReady", this.waitReady, pd.userStatus);
                this.wait = egret.setTimeout(this.sendReady, this, this.waitReady * 1000);
            }
            pd.chairID = d.C;
            pd.userStatus = d.S;
            if(App.userData.userStatus == UserStatus.NULL){
                if(App.gameView) App.gameView.onDestroy();
                App.gameView = null;
                // WebSocketInstance.getHallSocket().send(CMDConfig.SEND_CHANGESERVER, {});
            } else if(App.gameView){
                if(App.table.players[cid]){
                    let s = App.gameView.c2p(cid);
                    App.gameView.playerList[s] && App.gameView.playerList[s].status();
                }else App.gameView.showPlayer(cid);
            }
            return cid;
        }

        //////////////////////////////////////////////////////
        /** 发送准备 */
        public sendReady():void{ 
            if(App.userData.userStatus == UserStatus.SIT) App.net.send([6,2]);
        }
        /** 客户端进入游戏场景 */
        public enterGame():void{
            console.log("enterGame", D.App.userData.userStatus);
            this.waitReady = 3;
            //当前非游戏状态需要发送坐下
            if (D.App.userData.userStatus != UserStatus.PLAY && D.App.userData.userStatus != UserStatus.LOOKON) {
                App.net.send([8,1,{"wTableID":65535,"wChairID":0,"szTablePass":""}]);
            }else{ //同步桌子
                App.net.send([6,1,{"bAllowLookon":1}]);
            }
        }
    }
}